<template>
  <div class="department-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="group" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDelete(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 添加部门的弹层 -->
    <el-dialog title="添加部门" :visible.sync="showAddDialog" :close-on-click-modal="false" :close-on-press-escape="false" destroy-on-close>
      <AddorEdit v-if="showAddDialog" :id="pid" @Close="hClose" />
    </el-dialog>
    <!-- 编辑部门的弹层 -->
    <el-dialog title="编辑部门" :visible.sync="showEditDialog" :close-on-click-modal="false" :close-on-press-escape="false" destroy-on-close>
      <AddorEdit v-show="showEditDialog" :id="pid" :is-edit="isEdit" @Close="hClose" />
    </el-dialog>
  </div>
</template>
<script>
import { deleteDepart, getDepartmentList } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddorEdit from './deptDialog.vue'
export default {
  components: { AddorEdit },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showAddDialog: false,
      showEditDialog: false,
      pid: '',
      isEdit: true
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    // 获取所有的部门列表
    async getDepartmentList() {
      const { data } = await getDepartmentList()
      data.depts.shift()
      this.list = tranListToTreeData(data.depts)
    },
    // 添加部门
    hAdd(id) {
      this.showAddDialog = true
      this.pid = id
    },
    success() {
      this.showAddDialog = false
      this.showEditDialog = false
      this.getDepartmentList()
    },
    // 编辑部门
    hEdit(id) {
      this.showEditDialog = true
      this.pid = id
    },
    // 删除部门
    async hDelete(id) {
      const result = await this.$confirm('此操作将永久删除该部门，确定吗？', '提示', { type: 'warning' }).catch(() => { })
      if (!result === 'confirm') return
      try {
        const res = await deleteDepart(id)
        this.$message.success(res.message)
        this.getDepartmentList()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 关闭 dialog
    hClose() {
      this.showAddDialog = false
      this.showEditDialog = false
    }
  }
}
</script>
<style scope>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
